
package com.bear.cmcc.NetTestHTTP.db;

import com.bear.cmcc.NetTestHTTP.component.NetTestCellInfoBean;
import com.bear.cmcc.NetTestHTTP.component.NetTestConnecInfoBean;
import com.bear.cmcc.NetTestHTTP.component.NetTestRoundInfoBean;
import com.bear.cmcc.NetTestHTTP.component.NetTestTimesInfoBean;
import com.bear.cmcc.NetTestHTTP.constant.NetTestDBConstant;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import java.util.ArrayList;

/**
 * 测试下载服务数据库操作类
 * 
 * @author guanyh2013-2-28创建类
 * @add guanyh2013-3-1 增加创建表、插入表方法
 * @add guanyh2013-3-2 增加查询轮、次、小区、掉线率的表方法
 */
public class NetTestDBControl extends SQLiteOpenHelper {
    String TAG = "NetTestDBControl";

    /**
     * 创建数据库
     * 
     * @param context 父容器
     * @param _dbname 库名
     * @param _version 版本号
     */
    public NetTestDBControl(Context context, String _dbname, int _version) {
        // 创建数据库
        super(context, _dbname, null, _version);
    }

    public NetTestDBControl(Context context) {
        // 创建数据库
        super(context, NetTestDBConstant.DBNAME, null, NetTestDBConstant.DBVERSION);

    }

    /*
     * 创建测试round表
     */
    private void createTestRoundInfoTable(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE " + NetTestDBConstant.TABLES_TESTROUNDINFO_NAME + " ("
                + NetTestDBConstant.TestRoundFiledName.pid + " INTEGER PRIMARY KEY AUTOINCREMENT,"
                + NetTestDBConstant.TestRoundFiledName.startTime + " TEXT,"
                + NetTestDBConstant.TestRoundFiledName.endTime + " TEXT,"
                + NetTestDBConstant.TestRoundFiledName.averageSpeed + " TEXT,"
                + NetTestDBConstant.TestRoundFiledName.up_averageSpeed + " TEXT,"
                + NetTestDBConstant.TestRoundFiledName.maxSpeed + " TEXT,"
                + NetTestDBConstant.TestRoundFiledName.minSpeed + " TEXT,"
                + NetTestDBConstant.TestRoundFiledName.testTimes + " TEXT,"
                + NetTestDBConstant.TestRoundFiledName.isDelete + " TEXT,"
                + NetTestDBConstant.TestRoundFiledName.downServer + " TEXT,"
                + NetTestDBConstant.TestRoundFiledName.consumeTime + " TEXT" + ");");
    }

    /*
     * 创建测试time表
     */
    private void createTestTimesInfoTable(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE " + NetTestDBConstant.TABLES_TESTTIMEINFO_NAME + " ("
                + NetTestDBConstant.TestTimesFiledName.pid + " INTEGER PRIMARY KEY AUTOINCREMENT,"
                + NetTestDBConstant.TestTimesFiledName.downloadTime + " TEXT,"
                + NetTestDBConstant.TestTimesFiledName.fileSize + " TEXT,"
                + NetTestDBConstant.TestTimesFiledName.averageSpeed + " TEXT,"
                + NetTestDBConstant.TestTimesFiledName.maxSpeed + " TEXT,"
                + NetTestDBConstant.TestTimesFiledName.minSpeed + " TEXT,"
                + NetTestDBConstant.TestTimesFiledName.downServer + " TEXT,"
                + NetTestDBConstant.TestTimesFiledName.waveShakeStrong + " TEXT,"
                + NetTestDBConstant.TestTimesFiledName.roundID + " TEXT,"
                + NetTestDBConstant.TestTimesFiledName.replayCode + " TEXT,"
                + NetTestDBConstant.TestTimesFiledName.errorReason + " TEXT,"
                + NetTestDBConstant.TestTimesFiledName.isDelete + " TEXT,"
                + NetTestDBConstant.TestTimesFiledName.netType + " TEXT" + ");");
    }

    /*
     * 创建测试time表(上行)
     */
    private void createUpTestTimesInfoTable(SQLiteDatabase db) {
        Log.i(TAG, "TABLES_TESTTIMEINFO_NAME_UP");
        db.execSQL("CREATE TABLE " + NetTestDBConstant.TABLES_TESTTIMEINFO_NAME_UP + " ("
                + NetTestDBConstant.TestTimesFiledName.pid + " INTEGER PRIMARY KEY AUTOINCREMENT,"
                + NetTestDBConstant.TestTimesFiledName.downloadTime + " TEXT,"
                + NetTestDBConstant.TestTimesFiledName.fileSize + " TEXT,"
                + NetTestDBConstant.TestTimesFiledName.averageSpeed + " TEXT,"
                + NetTestDBConstant.TestTimesFiledName.maxSpeed + " TEXT,"
                + NetTestDBConstant.TestTimesFiledName.minSpeed + " TEXT,"
                + NetTestDBConstant.TestTimesFiledName.downServer + " TEXT,"
                + NetTestDBConstant.TestTimesFiledName.waveShakeStrong + " TEXT,"
                + NetTestDBConstant.TestTimesFiledName.roundID + " TEXT,"
                + NetTestDBConstant.TestTimesFiledName.replayCode + " TEXT,"
                + NetTestDBConstant.TestTimesFiledName.errorReason + " TEXT,"
                + NetTestDBConstant.TestTimesFiledName.isDelete + " TEXT,"
                + NetTestDBConstant.TestTimesFiledName.netType + " TEXT" + ");");
    }

    /*
     * 创建测试cell表
     */
    private void createTestCellInfoTable(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE " + NetTestDBConstant.TABLES_TESTCELLINFO_NAME + " ("
                + NetTestDBConstant.CellInfoFiledName.pid + " INTEGER PRIMARY KEY AUTOINCREMENT,"
                + NetTestDBConstant.CellInfoFiledName.averageSpeed + " TEXT,"
                + NetTestDBConstant.CellInfoFiledName.maxSpeed + " TEXT,"
                + NetTestDBConstant.CellInfoFiledName.minSpeed + " TEXT,"
                + NetTestDBConstant.CellInfoFiledName.lac + " TEXT,"
                + NetTestDBConstant.CellInfoFiledName.ci + " TEXT,"
                + NetTestDBConstant.CellInfoFiledName.cellName + " TEXT,"
                + NetTestDBConstant.CellInfoFiledName.maxNetStrong + " TEXT,"
                + NetTestDBConstant.CellInfoFiledName.minNetStrong + " TEXT,"
                + NetTestDBConstant.CellInfoFiledName.timesID + " TEXT,"
                + NetTestDBConstant.CellInfoFiledName.isDelete + " TEXT" + ");");
    }

    /*
     * 创建测试connect表
     */
    private void createTestConnectInfoTable(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE " + NetTestDBConstant.TABLES_CONNECTINFO_NAME + " ("
                + NetTestDBConstant.ConnectFiledName.pid + " INTEGER PRIMARY KEY AUTOINCREMENT,"
                + NetTestDBConstant.ConnectFiledName.connectPercent + " TEXT,"
                + NetTestDBConstant.ConnectFiledName.dropPercent + " TEXT,"
                + NetTestDBConstant.ConnectFiledName.lac + " TEXT,"
                + NetTestDBConstant.ConnectFiledName.ci + " TEXT,"
                + NetTestDBConstant.ConnectFiledName.isDelete + " TEXT" + ");");
    }

    /**
     * 重新创建表
     */
    private void createTestInfoTables(SQLiteDatabase db) {
        // if (!tabbleIsExist(NetTestDBConstant.TABLES_TESTROUNDINFO_NAME)) {
        // //没有这个字段说明是旧库，删掉
        // if(!queryField(NetTestDBConstant.TABLES_TESTROUNDINFO_NAME,NetTestDBConstant.TestRoundFiledName.consumeTime))
        // {
        Log.i(TAG, "begin createTestInfoTables ");
        try {
            db.execSQL("DROP TABLE IF EXISTS " + NetTestDBConstant.TABLES_TESTROUNDINFO_NAME);
            db.execSQL("DROP TABLE IF EXISTS " + NetTestDBConstant.TABLES_TESTTIMEINFO_NAME);
            db.execSQL("DROP TABLE IF EXISTS " + NetTestDBConstant.TABLES_TESTCELLINFO_NAME);
            db.execSQL("DROP TABLE IF EXISTS " + NetTestDBConstant.TABLES_CONNECTINFO_NAME);
            db.execSQL("DROP TABLE IF EXISTS " + NetTestDBConstant.TABLES_TESTTIMEINFO_NAME_UP);
            createTestRoundInfoTable(db);
            createTestTimesInfoTable(db);
            createTestCellInfoTable(db);
            createTestConnectInfoTable(db);
            createUpTestTimesInfoTable(db);
            Log.i(TAG, "end createTestInfoTables ");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        // }
        // }
    }

    public void reCreateTables() {
        SQLiteDatabase db = null;
        try {
            db = this.getWritableDatabase();
            db.execSQL("DROP TABLE IF EXISTS " + NetTestDBConstant.TABLES_TESTROUNDINFO_NAME);
            db.execSQL("DROP TABLE IF EXISTS " + NetTestDBConstant.TABLES_TESTTIMEINFO_NAME);
            db.execSQL("DROP TABLE IF EXISTS " + NetTestDBConstant.TABLES_TESTCELLINFO_NAME);
            db.execSQL("DROP TABLE IF EXISTS " + NetTestDBConstant.TABLES_CONNECTINFO_NAME);
            db.execSQL("DROP TABLE IF EXISTS " + NetTestDBConstant.TABLES_TESTTIMEINFO_NAME_UP);
            createTestRoundInfoTable(db);
            createTestTimesInfoTable(db);
            createTestCellInfoTable(db);
            createTestConnectInfoTable(db);
            createUpTestTimesInfoTable(db);
        } catch (SQLException e) {
            Log.d(TAG, "reCreateTables Exception=>" + e.getMessage());
        } finally {
            try {
                if (db != null) {
                    db.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 插入一条记录到round表
     * 
     * @param roundInfoBean
     * @return
     */
    public boolean insertToTestRoundInfo(NetTestRoundInfoBean roundInfoBean) {
        SQLiteDatabase db = null;
        try {
            db = this.getWritableDatabase();
            String sql = "";
            if (roundInfoBean.getPID() == 0) {
                sql = "insert into " + NetTestDBConstant.TABLES_TESTROUNDINFO_NAME + " ("
                        + NetTestDBConstant.TestRoundFiledName.startTime + ","
                        + NetTestDBConstant.TestRoundFiledName.endTime + ","
                        + NetTestDBConstant.TestRoundFiledName.averageSpeed + ","
                        + NetTestDBConstant.TestRoundFiledName.maxSpeed + ","
                        + NetTestDBConstant.TestRoundFiledName.minSpeed + ","
                        + NetTestDBConstant.TestRoundFiledName.testTimes + ","
                        + NetTestDBConstant.TestRoundFiledName.isDelete + ","
                        + NetTestDBConstant.TestRoundFiledName.downServer + ","
                        + NetTestDBConstant.TestRoundFiledName.consumeTime + ") values ('"
                        + roundInfoBean.getStartTime() + "','" + roundInfoBean.getEndTime() + "','"
                        + roundInfoBean.getAverageSpeed() + "','" + roundInfoBean.getMaxSpeed()
                        + "','" + roundInfoBean.getMinSpeed() + "','"
                        + roundInfoBean.getTestTimes() + "','" + "0" + "','"
                        + roundInfoBean.getDownServer() + "','" + roundInfoBean.getConsumeTime()
                        + "')";
            } else {
                sql = "insert into " + NetTestDBConstant.TABLES_TESTROUNDINFO_NAME + " ("
                        + NetTestDBConstant.TestRoundFiledName.pid + ","
                        + NetTestDBConstant.TestRoundFiledName.startTime + ","
                        + NetTestDBConstant.TestRoundFiledName.endTime + ","
                        + NetTestDBConstant.TestRoundFiledName.averageSpeed + ","
                        + NetTestDBConstant.TestRoundFiledName.maxSpeed + ","
                        + NetTestDBConstant.TestRoundFiledName.minSpeed + ","
                        + NetTestDBConstant.TestRoundFiledName.testTimes + ","
                        + NetTestDBConstant.TestRoundFiledName.isDelete + ","
                        + NetTestDBConstant.TestRoundFiledName.downServer + ","
                        + NetTestDBConstant.TestRoundFiledName.consumeTime + ") values ('"
                        + roundInfoBean.getPID() + "','" + roundInfoBean.getStartTime() + "','"
                        + roundInfoBean.getEndTime() + "','" + roundInfoBean.getAverageSpeed()
                        + "','" + roundInfoBean.getMaxSpeed() + "','" + roundInfoBean.getMinSpeed()
                        + "','" + roundInfoBean.getTestTimes() + "','" + "0" + "','"
                        + roundInfoBean.getDownServer() + "','" + roundInfoBean.getConsumeTime()
                        + "')";
            }
            db.execSQL(sql);
            return true;
        } catch (SQLException e) {
            Log.d(TAG, "insertToTestRoundInfo Exception=>" + e.getMessage());
            return false;
        } finally {
            try {
                if (db != null) {
                    db.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 更新一条记录到round表
     * 
     * @param roundInfoBean
     * @return
     */
    public boolean updateOrCreateTestRoundInfo(NetTestRoundInfoBean roundInfoBean) {
        SQLiteDatabase db = null;
        try {
            db = this.getWritableDatabase();
            String sql = "";
            if (roundInfoBean.getPID() == 0) {
                sql = "insert into " + NetTestDBConstant.TABLES_TESTROUNDINFO_NAME + " ("
                        + NetTestDBConstant.TestRoundFiledName.startTime + ","
                        + NetTestDBConstant.TestRoundFiledName.endTime + ","
                        + NetTestDBConstant.TestRoundFiledName.averageSpeed + ","
                        + NetTestDBConstant.TestRoundFiledName.up_averageSpeed + ","
                        + NetTestDBConstant.TestRoundFiledName.maxSpeed + ","
                        + NetTestDBConstant.TestRoundFiledName.minSpeed + ","
                        + NetTestDBConstant.TestRoundFiledName.testTimes + ","
                        + NetTestDBConstant.TestRoundFiledName.isDelete + ","
                        + NetTestDBConstant.TestRoundFiledName.downServer + ","
                        + NetTestDBConstant.TestRoundFiledName.consumeTime + ") values ('"
                        + roundInfoBean.getStartTime() + "','" + roundInfoBean.getEndTime() + "','"
                        + roundInfoBean.getAverageSpeed() + "','"
                        + roundInfoBean.getUp_averageSpeed() + "','" + roundInfoBean.getMaxSpeed()
                        + "','" + roundInfoBean.getMinSpeed() + "','"
                        + roundInfoBean.getTestTimes() + "','" + "0" + "','"
                        + roundInfoBean.getDownServer() + "','" + roundInfoBean.getConsumeTime()
                        + "')";
            } else {
                sql = "update " + NetTestDBConstant.TABLES_TESTROUNDINFO_NAME + " set "
                        + NetTestDBConstant.TestRoundFiledName.startTime + " = '"
                        + roundInfoBean.getStartTime() + "',"
                        + NetTestDBConstant.TestRoundFiledName.endTime + " = '"
                        + roundInfoBean.getEndTime() + "',"
                        + NetTestDBConstant.TestRoundFiledName.averageSpeed + " = '"
                        + roundInfoBean.getAverageSpeed() + "',"
                        + NetTestDBConstant.TestRoundFiledName.up_averageSpeed + " = '"
                        + roundInfoBean.getUp_averageSpeed() + "',"
                        + NetTestDBConstant.TestRoundFiledName.maxSpeed + " = '"
                        + roundInfoBean.getMaxSpeed() + "',"
                        + NetTestDBConstant.TestRoundFiledName.minSpeed + " = '"
                        + roundInfoBean.getMinSpeed() + "',"
                        + NetTestDBConstant.TestRoundFiledName.testTimes + " = '"
                        + roundInfoBean.getTestTimes() + "',"
                        + NetTestDBConstant.TestRoundFiledName.isDelete + " = " + "'0'" + ","
                        + NetTestDBConstant.TestRoundFiledName.downServer + " = '"
                        + roundInfoBean.getDownServer() + "',"
                        + NetTestDBConstant.TestRoundFiledName.consumeTime + " = '"
                        + roundInfoBean.getConsumeTime() + "'  where "
                        + NetTestDBConstant.TestRoundFiledName.pid + " = '"
                        + roundInfoBean.getPID() + "'";

            }
            db.execSQL(sql);
            return true;
        } catch (SQLException e) {
            Log.d(TAG, "updateOrCreateTestRoundInfo Exception=>" + e.getMessage());
            return false;
        } finally {
            try {
                if (db != null) {
                    db.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 插入一条记录到times表
     * 
     * @param timeInfoBean
     * @return
     */
    public boolean insertToTestTimesInfo(NetTestTimesInfoBean timeInfoBean) {
        SQLiteDatabase db = null;
        try {
            db = getWritableDatabase();
            String sql = "";
            if (timeInfoBean.getPID() == 0) {
                sql = "insert into " + NetTestDBConstant.TABLES_TESTTIMEINFO_NAME + " ("
                        + NetTestDBConstant.TestTimesFiledName.downloadTime + ","
                        + NetTestDBConstant.TestTimesFiledName.fileSize + ","
                        + NetTestDBConstant.TestTimesFiledName.averageSpeed + ","
                        + NetTestDBConstant.TestTimesFiledName.maxSpeed + ","
                        + NetTestDBConstant.TestTimesFiledName.minSpeed + ","
                        + NetTestDBConstant.TestTimesFiledName.downServer + ","
                        + NetTestDBConstant.TestTimesFiledName.waveShakeStrong + ","
                        + NetTestDBConstant.TestTimesFiledName.roundID + ","
                        + NetTestDBConstant.TestTimesFiledName.replayCode + ","
                        + NetTestDBConstant.TestTimesFiledName.errorReason + ","
                        + NetTestDBConstant.TestTimesFiledName.isDelete + ","
                        + NetTestDBConstant.TestTimesFiledName.netType + ") values ('"
                        + timeInfoBean.getDownLoadTime() + "','" + timeInfoBean.getFileSize()
                        + "','" + timeInfoBean.getAverageSpeed() + "','"
                        + timeInfoBean.getMaxSpeed() + "','" + timeInfoBean.getMinSpeed() + "','"
                        + timeInfoBean.getDownServer() + "','" + timeInfoBean.getWaveShakeStrong()
                        + "','" + timeInfoBean.getRoundID() + "','" + timeInfoBean.getReplayCode()
                        + "','" + timeInfoBean.getErrorReason() + "','" + "0" + "','"
                        + timeInfoBean.getNetType() + "')";
            } else {
                sql = "insert into " + NetTestDBConstant.TABLES_TESTTIMEINFO_NAME + " ("
                        + NetTestDBConstant.TestTimesFiledName.pid + ","
                        + NetTestDBConstant.TestTimesFiledName.downloadTime + ","
                        + NetTestDBConstant.TestTimesFiledName.fileSize + ","
                        + NetTestDBConstant.TestTimesFiledName.averageSpeed + ","
                        + NetTestDBConstant.TestTimesFiledName.maxSpeed + ","
                        + NetTestDBConstant.TestTimesFiledName.minSpeed + ","
                        + NetTestDBConstant.TestTimesFiledName.downServer + ","
                        + NetTestDBConstant.TestTimesFiledName.waveShakeStrong + ","
                        + NetTestDBConstant.TestTimesFiledName.roundID + ","
                        + NetTestDBConstant.TestTimesFiledName.replayCode + ","
                        + NetTestDBConstant.TestTimesFiledName.errorReason + ","
                        + NetTestDBConstant.TestTimesFiledName.isDelete + ","
                        + NetTestDBConstant.TestTimesFiledName.netType + ") values ('"
                        + timeInfoBean.getPID() + "','" + timeInfoBean.getDownLoadTime() + "','"
                        + timeInfoBean.getFileSize() + "','" + timeInfoBean.getAverageSpeed()
                        + "','" + timeInfoBean.getMaxSpeed() + "','" + timeInfoBean.getMinSpeed()
                        + "','" + timeInfoBean.getDownServer() + "','"
                        + timeInfoBean.getWaveShakeStrong() + "','" + timeInfoBean.getRoundID()
                        + "','" + timeInfoBean.getReplayCode() + "','"
                        + timeInfoBean.getErrorReason() + "','" + "0" + "','"
                        + timeInfoBean.getNetType() + "')";
            }
            db.execSQL(sql);
            return true;
        } catch (SQLException e) {
            Log.d(TAG, "insertToTestTimesInfo Exception=>" + e.getMessage());
            return false;
        } finally {
            try {
                if (db != null) {
                    db.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 插入一条记录到times表
     * 
     * @param timeInfoBean
     * @return
     */
    public boolean insertToUpTestTimesInfo(NetTestTimesInfoBean timeInfoBean) {
        SQLiteDatabase db = null;
        try {
            db = getWritableDatabase();
            String sql = "";
            if (timeInfoBean.getPID() == 0) {
                sql = "insert into " + NetTestDBConstant.TABLES_TESTTIMEINFO_NAME_UP + " ("
                        + NetTestDBConstant.TestTimesFiledName.downloadTime + ","
                        + NetTestDBConstant.TestTimesFiledName.fileSize + ","
                        + NetTestDBConstant.TestTimesFiledName.averageSpeed + ","
                        + NetTestDBConstant.TestTimesFiledName.maxSpeed + ","
                        + NetTestDBConstant.TestTimesFiledName.minSpeed + ","
                        + NetTestDBConstant.TestTimesFiledName.downServer + ","
                        + NetTestDBConstant.TestTimesFiledName.waveShakeStrong + ","
                        + NetTestDBConstant.TestTimesFiledName.roundID + ","
                        + NetTestDBConstant.TestTimesFiledName.replayCode + ","
                        + NetTestDBConstant.TestTimesFiledName.errorReason + ","
                        + NetTestDBConstant.TestTimesFiledName.isDelete + ","
                        + NetTestDBConstant.TestTimesFiledName.netType + ") values ('"
                        + timeInfoBean.getDownLoadTime() + "','" + timeInfoBean.getFileSize()
                        + "','" + timeInfoBean.getAverageSpeed() + "','"
                        + timeInfoBean.getMaxSpeed() + "','" + timeInfoBean.getMinSpeed() + "','"
                        + timeInfoBean.getDownServer() + "','" + timeInfoBean.getWaveShakeStrong()
                        + "','" + timeInfoBean.getRoundID() + "','" + timeInfoBean.getReplayCode()
                        + "','" + timeInfoBean.getErrorReason() + "','" + "0" + "','"
                        + timeInfoBean.getNetType() + "')";
            } else {
                sql = "insert into " + NetTestDBConstant.TABLES_TESTTIMEINFO_NAME_UP + " ("
                        + NetTestDBConstant.TestTimesFiledName.pid + ","
                        + NetTestDBConstant.TestTimesFiledName.downloadTime + ","
                        + NetTestDBConstant.TestTimesFiledName.fileSize + ","
                        + NetTestDBConstant.TestTimesFiledName.averageSpeed + ","
                        + NetTestDBConstant.TestTimesFiledName.maxSpeed + ","
                        + NetTestDBConstant.TestTimesFiledName.minSpeed + ","
                        + NetTestDBConstant.TestTimesFiledName.downServer + ","
                        + NetTestDBConstant.TestTimesFiledName.waveShakeStrong + ","
                        + NetTestDBConstant.TestTimesFiledName.roundID + ","
                        + NetTestDBConstant.TestTimesFiledName.replayCode + ","
                        + NetTestDBConstant.TestTimesFiledName.errorReason + ","
                        + NetTestDBConstant.TestTimesFiledName.isDelete + ","
                        + NetTestDBConstant.TestTimesFiledName.netType + ") values ('"
                        + timeInfoBean.getPID() + "','" + timeInfoBean.getDownLoadTime() + "','"
                        + timeInfoBean.getFileSize() + "','" + timeInfoBean.getAverageSpeed()
                        + "','" + timeInfoBean.getMaxSpeed() + "','" + timeInfoBean.getMinSpeed()
                        + "','" + timeInfoBean.getDownServer() + "','"
                        + timeInfoBean.getWaveShakeStrong() + "','" + timeInfoBean.getRoundID()
                        + "','" + timeInfoBean.getReplayCode() + "','"
                        + timeInfoBean.getErrorReason() + "','" + "0" + "','"
                        + timeInfoBean.getNetType() + "')";
            }
            db.execSQL(sql);
            return true;
        } catch (SQLException e) {
            Log.d(TAG, "insertTo  Up TestTimesInfo Exception=>" + e.getMessage());
            return false;
        } finally {
            try {
                if (db != null) {
                    db.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 插入一条记录到cell表
     * 
     * @param cellInfoBean
     * @return
     */
    public boolean insertToTestCellInfo(NetTestCellInfoBean cellInfoBean) {
        SQLiteDatabase db = null;
        try {
            db = getWritableDatabase();
            String sql = "";
            if (cellInfoBean.getPID() == 0) {
                sql = "insert into " + NetTestDBConstant.TABLES_TESTCELLINFO_NAME + " ("
                        + NetTestDBConstant.CellInfoFiledName.averageSpeed + ","
                        + NetTestDBConstant.CellInfoFiledName.maxSpeed + ","
                        + NetTestDBConstant.CellInfoFiledName.minSpeed + ","
                        + NetTestDBConstant.CellInfoFiledName.lac + ","
                        + NetTestDBConstant.CellInfoFiledName.ci + ","
                        + NetTestDBConstant.CellInfoFiledName.cellName + ","
                        + NetTestDBConstant.CellInfoFiledName.maxNetStrong + ","
                        + NetTestDBConstant.CellInfoFiledName.minNetStrong + ","
                        + NetTestDBConstant.CellInfoFiledName.timesID + ","
                        + NetTestDBConstant.CellInfoFiledName.isDelete + ") values ('"
                        + cellInfoBean.getAverageSpeed() + "','" + cellInfoBean.getMaxSpeed()
                        + "','" + cellInfoBean.getMinSpeed() + "','" + cellInfoBean.getLac()
                        + "','" + cellInfoBean.getCi() + "','" + cellInfoBean.getCellName() + "','"
                        + cellInfoBean.getMaxNetStrong() + "','" + cellInfoBean.getMinNetStrong()
                        + "','" + cellInfoBean.getTimesID() + "','" + "0" + "')";
            } else {
                sql = "insert into " + NetTestDBConstant.TABLES_TESTCELLINFO_NAME + " ("
                        + NetTestDBConstant.CellInfoFiledName.pid + ","
                        + NetTestDBConstant.CellInfoFiledName.averageSpeed + ","
                        + NetTestDBConstant.CellInfoFiledName.maxSpeed + ","
                        + NetTestDBConstant.CellInfoFiledName.minSpeed + ","
                        + NetTestDBConstant.CellInfoFiledName.lac + ","
                        + NetTestDBConstant.CellInfoFiledName.ci + ","
                        + NetTestDBConstant.CellInfoFiledName.cellName + ","
                        + NetTestDBConstant.CellInfoFiledName.maxNetStrong + ","
                        + NetTestDBConstant.CellInfoFiledName.minNetStrong + ","
                        + NetTestDBConstant.CellInfoFiledName.timesID + ","
                        + NetTestDBConstant.CellInfoFiledName.isDelete + ") values ('"
                        + cellInfoBean.getPID() + "','" + cellInfoBean.getAverageSpeed() + "','"
                        + cellInfoBean.getMaxSpeed() + "','" + cellInfoBean.getMinSpeed() + "','"
                        + cellInfoBean.getLac() + "','" + cellInfoBean.getCi() + "','"
                        + cellInfoBean.getCellName() + "','" + cellInfoBean.getMaxNetStrong()
                        + "','" + cellInfoBean.getMinNetStrong() + "','"
                        + cellInfoBean.getTimesID() + "','" + "0" + "')";
            }
            db.execSQL(sql);
            return true;
        } catch (SQLException e) {
            Log.d(TAG, "insertToTestCellInfo Exception=>" + e.getMessage());
            return false;
        } finally {
            try {
                if (db != null) {
                    db.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 插入一条记录到connect表
     * 
     * @param cellInfoBean
     * @return
     */
    public boolean insertToTestConnectInfo(NetTestConnecInfoBean connectInfoBean) {
        SQLiteDatabase db = null;
        try {
            db = getWritableDatabase();
            String sql = "";
            if (connectInfoBean.getPID() == 0) {
                sql = "insert into " + NetTestDBConstant.TABLES_CONNECTINFO_NAME + " ("
                        + NetTestDBConstant.ConnectFiledName.dropPercent + ","
                        + NetTestDBConstant.ConnectFiledName.connectPercent + ","
                        + NetTestDBConstant.ConnectFiledName.lac + ","
                        + NetTestDBConstant.ConnectFiledName.ci + ","
                        + NetTestDBConstant.ConnectFiledName.isDelete + ") values ('"
                        + connectInfoBean.getDropPercent() + "','"
                        + connectInfoBean.getConnectPercent() + "','" + connectInfoBean.getLac()
                        + "','" + connectInfoBean.getCi() + "','" + "0" + "')";
            } else {
                sql = "insert into " + NetTestDBConstant.TABLES_CONNECTINFO_NAME + " ("
                        + NetTestDBConstant.ConnectFiledName.pid + ","
                        + NetTestDBConstant.ConnectFiledName.dropPercent + ","
                        + NetTestDBConstant.ConnectFiledName.connectPercent + ","
                        + NetTestDBConstant.ConnectFiledName.lac + ","
                        + NetTestDBConstant.ConnectFiledName.ci + ","
                        + NetTestDBConstant.ConnectFiledName.isDelete + ") values ('"
                        + connectInfoBean.getPID() + "','" + connectInfoBean.getDropPercent()
                        + "','" + connectInfoBean.getConnectPercent() + "','"
                        + connectInfoBean.getLac() + "','" + connectInfoBean.getCi() + "','" + "0"
                        + "')";
            }
            db.execSQL(sql);
            return true;
        } catch (SQLException e) {
            Log.d(TAG, "insertToTestConnectInfo Exception=>" + e.getMessage());
            return false;
        } finally {
            try {
                if (db != null) {
                    db.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 根据pid获取一轮round表的信息
     * 
     * @param _pid
     * @return
     */
    public NetTestRoundInfoBean queryRoundInfoByPid(String _pid) {
        Cursor cursor = null;
        SQLiteDatabase db = null;
        try {
            db = getReadableDatabase();
            String sql = "select * from " + NetTestDBConstant.TABLES_TESTROUNDINFO_NAME
                    + " where pid =?";
            cursor = db.rawQuery(sql, new String[] {
                _pid
            });
            if (cursor != null && cursor.getCount() > 0) {
                cursor.moveToFirst();
                return dbSetRoundInfoBean(cursor);
            } else
                return null;
        } catch (Exception e) {
            Log.d(TAG, "queryRoundInfoByPid Exception=>" + e.getMessage());
            return null;
        } finally {
            try {
                if (cursor != null) {
                    cursor.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                if (db != null) {
                    db.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 根据pid获取一轮round表的信息
     * 
     * @param _pid
     * @return
     */
    public NetTestRoundInfoBean queryRoundInfoByStartTime(String startTime) {
        Cursor cursor = null;
        SQLiteDatabase db = null;
        try {
            db = getReadableDatabase();
            String sql = "select * from " + NetTestDBConstant.TABLES_TESTROUNDINFO_NAME + " where "
                    + NetTestDBConstant.TestRoundFiledName.startTime + " =?";
            cursor = db.rawQuery(sql, new String[] {
                startTime
            });

            if (cursor != null && cursor.getCount() > 0) {
                cursor.moveToFirst();
                return dbSetRoundInfoBean(cursor);
            } else
                return null;
        } catch (Exception e) {
            Log.d(TAG, "queryRoundInfoByPid Exception=>" + e.getMessage());
            return null;
        } finally {
            try {
                if (cursor != null) {
                    cursor.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                if (db != null) {
                    db.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 返回round表所有记录，即所有的轮测试的信息(降序排序)
     * 
     * @param needsDelete 如果false只返回未删除过的round信息，如果true返回全量
     * @return
     */
    public ArrayList<NetTestRoundInfoBean> queryAllRoundInfo(boolean needsDelete) {
        Cursor cursor = null;
        SQLiteDatabase db = null;
        ArrayList<NetTestRoundInfoBean> netTestRoundInfoBeans = new ArrayList<NetTestRoundInfoBean>();
        try {
            db = getReadableDatabase();
            String sql = "select * from " + NetTestDBConstant.TABLES_TESTROUNDINFO_NAME
                    + " order by pid desc";
            cursor = db.rawQuery(sql, null);

            if (cursor != null && cursor.getCount() > 0) {
                if (needsDelete) {
                    while (cursor.moveToNext()) {
                        NetTestRoundInfoBean roundInfoBean = dbSetRoundInfoBean(cursor);
                        netTestRoundInfoBeans.add(roundInfoBean);
                    }
                } else {
                    while (cursor.moveToNext()) {
                        if (cursor
                                .getString(
                                        cursor.getColumnIndex(NetTestDBConstant.TestRoundFiledName.isDelete))
                                .equals("0")) {
                            NetTestRoundInfoBean roundInfoBean = dbSetRoundInfoBean(cursor);
                            netTestRoundInfoBeans.add(roundInfoBean);
                        }
                    }
                }
                if (netTestRoundInfoBeans.size() > 0)
                    return netTestRoundInfoBeans;
                else
                    return null;
            } else
                return null;
        } catch (Exception e) {
            Log.d(TAG, "queryAllRoundInfo Exception=>" + e.getMessage());
            return null;
        } finally {
            try {
                if (cursor != null) {
                    cursor.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                if (db != null) {
                    db.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 根据一次cursor返回roundInfoBean对象
     * 
     * @param cursor
     * @param roundInfoBean
     */
    private NetTestRoundInfoBean dbSetRoundInfoBean(Cursor cursor) {
        NetTestRoundInfoBean roundInfoBean = new NetTestRoundInfoBean();
        roundInfoBean.setPID(cursor.getInt(cursor
                .getColumnIndex(NetTestDBConstant.TestRoundFiledName.pid)));
        roundInfoBean.setStartTime(cursor.getString(cursor
                .getColumnIndex(NetTestDBConstant.TestRoundFiledName.startTime)));
        roundInfoBean.setEndTime(cursor.getString(cursor
                .getColumnIndex(NetTestDBConstant.TestRoundFiledName.endTime)));
        try {
            roundInfoBean.setAverageSpeed(Double.parseDouble(cursor.getString(cursor
                    .getColumnIndex(NetTestDBConstant.TestRoundFiledName.averageSpeed))));
        } catch (Exception e) {
            roundInfoBean.setAverageSpeed(0.0);
        }
        try {
            roundInfoBean.setUp_averageSpeed(Double.parseDouble(cursor.getString(cursor
                    .getColumnIndex(NetTestDBConstant.TestRoundFiledName.up_averageSpeed))));
        } catch (Exception e) {
            roundInfoBean.setUp_averageSpeed(0.0);
        }
        try {
            roundInfoBean.setMaxSpeed(Double.parseDouble(cursor.getString(cursor
                    .getColumnIndex(NetTestDBConstant.TestRoundFiledName.maxSpeed))));
        } catch (Exception e) {
            roundInfoBean.setMaxSpeed(0.0);
        }
        try {
            roundInfoBean.setMinSpeed(Double.parseDouble(cursor.getString(cursor
                    .getColumnIndex(NetTestDBConstant.TestRoundFiledName.minSpeed))));
        } catch (Exception e) {
            roundInfoBean.setMinSpeed(0.0);
        }
        try {
            roundInfoBean.setTestTimes(Integer.parseInt(cursor.getString(cursor
                    .getColumnIndex(NetTestDBConstant.TestRoundFiledName.testTimes))));
        } catch (Exception e) {
            roundInfoBean.setTestTimes(0);
        }
        roundInfoBean.setIsDelete(cursor.getString(cursor
                .getColumnIndex(NetTestDBConstant.TestRoundFiledName.isDelete)));
        roundInfoBean.setDownServer(cursor.getString(cursor
                .getColumnIndex(NetTestDBConstant.TestRoundFiledName.downServer)));
        roundInfoBean.setConsumeTime(cursor.getString(cursor
                .getColumnIndex(NetTestDBConstant.TestRoundFiledName.consumeTime)));
        return roundInfoBean;
    }

    /**
     * 根据pid获取一次测试信息即一条times表记录
     * 
     * @param _pid
     * @return
     */
    public NetTestTimesInfoBean queryTimesInfoByPid(String _pid) {
        Cursor cursor = null;
        SQLiteDatabase db = null;
        try {
            db = getReadableDatabase();
            String sql = "select * from " + NetTestDBConstant.TABLES_TESTTIMEINFO_NAME
                    + " where pid =?";
            cursor = db.rawQuery(sql, new String[] {
                _pid
            });

            if (cursor != null && cursor.getCount() > 0) {
                cursor.moveToFirst();
                return dbSetTimesInfoBean(cursor);
            } else
                return null;
        } catch (Exception e) {
            Log.d(TAG, "queryTimesInfoByPid Exception=>" + e.getMessage());
            return null;
        } finally {
            try {
                if (cursor != null) {
                    cursor.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                if (db != null) {
                    db.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 查询times表所有数据
     * 
     * @return
     */
    public ArrayList<NetTestTimesInfoBean> queryAllTimesInfos() {
        Cursor cursor = null;
        SQLiteDatabase db = null;
        ArrayList<NetTestTimesInfoBean> netTestTimesInfoBeans = new ArrayList<NetTestTimesInfoBean>();
        try {
            db = getReadableDatabase();
            String sql = "select * from " + NetTestDBConstant.TABLES_TESTTIMEINFO_NAME;
            cursor = db.rawQuery(sql, null);

            if (cursor != null && cursor.getCount() > 0) {
                while (cursor.moveToNext()) {
                    NetTestTimesInfoBean timesInfoBean = dbSetTimesInfoBean(cursor);
                    netTestTimesInfoBeans.add(timesInfoBean);
                }
                if (netTestTimesInfoBeans.size() > 0)
                    return netTestTimesInfoBeans;
                else
                    return null;
            } else
                return null;
        } catch (Exception e) {
            Log.d(TAG, "queryAllTimesInfos Exception=>" + e.getMessage());
            return null;
        } finally {
            try {
                if (cursor != null) {
                    cursor.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                if (db != null) {
                    db.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 获取一轮测试下所有次的信息
     * 
     * @param _roundID
     * @return
     */
    public ArrayList<NetTestTimesInfoBean> queryTimesInfoByRoundID(String _roundID) {
        SQLiteDatabase db = null;
        Cursor cursor = null;
        ArrayList<NetTestTimesInfoBean> netTestTimesInfoBeans = new ArrayList<NetTestTimesInfoBean>();
        try {
            db = getReadableDatabase();
            String sql = "select * from " + NetTestDBConstant.TABLES_TESTTIMEINFO_NAME + " where "
                    + NetTestDBConstant.TestTimesFiledName.roundID + " =? order by pid desc";
            cursor = db.rawQuery(sql, new String[] {
                _roundID
            });

            if (cursor != null && cursor.moveToFirst()) {
                do {
                    NetTestTimesInfoBean timesInfoBean = dbSetTimesInfoBean(cursor);
                    netTestTimesInfoBeans.add(timesInfoBean);
                } while (cursor.moveToNext());
                if (netTestTimesInfoBeans.size() > 0)
                    return netTestTimesInfoBeans;
                else
                    return null;
            } else
                return null;
        } catch (Exception e) {
            Log.d(TAG, "queryTimesInfoByRoundID Exception=>" + e.getMessage());
            return null;
        } finally {
            try {
                if (cursor != null) {
                    cursor.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                if (db != null) {
                    db.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 获取一轮测试下所有次的信息(上行测试次的信息)
     * 
     * @param _roundID
     * @return
     */
    public ArrayList<NetTestTimesInfoBean> queryUpTimesInfoByRoundID(String _roundID) {
        SQLiteDatabase db = null;
        Cursor cursor = null;
        ArrayList<NetTestTimesInfoBean> netTestTimesInfoBeans = new ArrayList<NetTestTimesInfoBean>();
        try {
            db = getReadableDatabase();
            String sql = "select * from " + NetTestDBConstant.TABLES_TESTTIMEINFO_NAME_UP
                    + " where " + NetTestDBConstant.TestTimesFiledName.roundID
                    + " =? order by pid desc";
            cursor = db.rawQuery(sql, new String[] {
                _roundID
            });

            if (cursor != null && cursor.getCount() > 0) {
                while (cursor.moveToNext()) {
                    NetTestTimesInfoBean timesInfoBean = dbSetTimesInfoBean(cursor);
                    netTestTimesInfoBeans.add(timesInfoBean);
                }
                if (netTestTimesInfoBeans.size() > 0)
                    return netTestTimesInfoBeans;
                else
                    return null;
            } else
                return null;
        } catch (Exception e) {
            Log.d(TAG, "query UpTimesInfoByRoundID Exception=>" + e.getMessage());
            return null;
        } finally {
            try {
                if (cursor != null) {
                    cursor.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                if (db != null) {
                    db.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 根据一次cursor返回timesInfoBean对象
     * 
     * @param cursor
     * @param roundInfoBean
     */
    private NetTestTimesInfoBean dbSetTimesInfoBean(Cursor cursor) {
        NetTestTimesInfoBean timesInfoBean = new NetTestTimesInfoBean();
        timesInfoBean.setPID(cursor.getInt(cursor
                .getColumnIndex(NetTestDBConstant.TestTimesFiledName.pid)));
        timesInfoBean.setDownLoadTime(cursor.getString(cursor
                .getColumnIndex(NetTestDBConstant.TestTimesFiledName.downloadTime)));
        timesInfoBean.setFileSize(cursor.getString(cursor
                .getColumnIndex(NetTestDBConstant.TestTimesFiledName.fileSize)));

        try {
            timesInfoBean.setAverageSpeed(Double.parseDouble(cursor.getString(cursor
                    .getColumnIndex(NetTestDBConstant.TestTimesFiledName.averageSpeed))));
        } catch (Exception e) {
            timesInfoBean.setAverageSpeed(0.0);
        }
        try {
            timesInfoBean.setMaxSpeed(Double.parseDouble(cursor.getString(cursor
                    .getColumnIndex(NetTestDBConstant.TestTimesFiledName.maxSpeed))));
        } catch (Exception e) {
            timesInfoBean.setMaxSpeed(0.0);
        }
        try {
            timesInfoBean.setMinSpeed(Double.parseDouble(cursor.getString(cursor
                    .getColumnIndex(NetTestDBConstant.TestTimesFiledName.minSpeed))));
        } catch (Exception e) {
            timesInfoBean.setMinSpeed(0.0);
        }
        timesInfoBean.setDownServer(cursor.getString(cursor
                .getColumnIndex(NetTestDBConstant.TestTimesFiledName.downServer)));
        try {
            timesInfoBean.setWaveShakeStrong(cursor.getDouble(cursor
                    .getColumnIndex(NetTestDBConstant.TestTimesFiledName.waveShakeStrong)));
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        try {
            timesInfoBean.setRoundID(Integer.parseInt(cursor.getString(cursor
                    .getColumnIndex(NetTestDBConstant.TestTimesFiledName.roundID))));
        } catch (Exception e) {
            timesInfoBean.setRoundID(0);
        }
        timesInfoBean.setIsDelete(cursor.getString(cursor
                .getColumnIndex(NetTestDBConstant.TestTimesFiledName.isDelete)));
        try {
            timesInfoBean.setReplayCode(Integer.parseInt(cursor.getString(cursor
                    .getColumnIndex(NetTestDBConstant.TestTimesFiledName.replayCode))));
        } catch (Exception e) {
            timesInfoBean.setReplayCode(0);
        }
        timesInfoBean.setErrorReason(cursor.getString(cursor
                .getColumnIndex(NetTestDBConstant.TestTimesFiledName.errorReason)));
        timesInfoBean.setIsDelete(cursor.getString(cursor
                .getColumnIndex(NetTestDBConstant.TestTimesFiledName.isDelete)));
        timesInfoBean.setNetType(cursor.getString(cursor
                .getColumnIndex(NetTestDBConstant.TestTimesFiledName.netType)));
        return timesInfoBean;
    }

    /**
     * 查询所有cell表的数据
     * 
     * @return
     */
    public ArrayList<NetTestCellInfoBean> queryAllCellInfos() {
        SQLiteDatabase db = null;
        Cursor cursor = null;
        ArrayList<NetTestCellInfoBean> netTestCellInfoBeans = new ArrayList<NetTestCellInfoBean>();
        try {
            db = getReadableDatabase();
            String sql = "select * from " + NetTestDBConstant.TABLES_TESTCELLINFO_NAME;
            cursor = db.rawQuery(sql, null);
            if (cursor != null && cursor.getCount() > 0) {
                // if (cursor!=null&&cursor.getCount() > 0) {
                while (cursor.moveToNext()) {
                    NetTestCellInfoBean cellInfoBean = dbSetCellInfoBean(cursor);
                    netTestCellInfoBeans.add(cellInfoBean);
                }
                if (netTestCellInfoBeans.size() > 0)
                    return netTestCellInfoBeans;
                else
                    return null;
            } else
                return null;
        } catch (Exception e) {
            Log.d(TAG, "queryAllCellInfos Exception=>" + e.getMessage());
            return null;
        } finally {
            try {
                if (cursor != null) {
                    cursor.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                if (db != null) {
                    db.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 查询一次测试所有的小区信息
     * 
     * @param _timeID
     * @return
     */
    public ArrayList<NetTestCellInfoBean> queryCellInfoBeanByTimesID(String _timeID) {
        SQLiteDatabase db = null;
        Cursor cursor = null;
        ArrayList<NetTestCellInfoBean> netTestCellInfoBeans = new ArrayList<NetTestCellInfoBean>();
        try {
            db = getReadableDatabase();
            String sql = "select * from " + NetTestDBConstant.TABLES_TESTCELLINFO_NAME + " where "
                    + NetTestDBConstant.CellInfoFiledName.timesID + " =? order by pid";
            cursor = db.rawQuery(sql, new String[] {
                _timeID
            });
            if (cursor != null && cursor.moveToFirst()) {
                do {
                    NetTestCellInfoBean cellInfoBean = dbSetCellInfoBean(cursor);
                    netTestCellInfoBeans.add(cellInfoBean);
                } while (cursor.moveToNext());
                if (netTestCellInfoBeans.size() > 0)
                    return netTestCellInfoBeans;
                else
                    return null;
            } else
                return null;
        } catch (Exception e) {
            Log.d(TAG, "queryCellInfoBeanByTimesID Exception=>" + e.getMessage());
            return null;
        } finally {
            try {
                if (cursor != null) {
                    cursor.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                if (db != null) {
                    db.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 根据lac、ci和timeid查询是否有这条小区记录
     * 
     * @param _lac
     * @param _ci
     * @param _timeID
     * @return
     */
    public boolean hasCellInfoBeanByTimesID(String _lac, String _ci, String _timeID) {
        SQLiteDatabase db = null;
        Cursor cursor = null;
        try {
            db = getReadableDatabase();
            String sql = "select * from " + NetTestDBConstant.TABLES_TESTCELLINFO_NAME + " where "
                    + NetTestDBConstant.CellInfoFiledName.timesID + " =? and "
                    + NetTestDBConstant.CellInfoFiledName.lac + " =? and "
                    + NetTestDBConstant.CellInfoFiledName.ci + " =? order by pid";
            cursor = db.rawQuery(sql, new String[] {
                    _timeID, _lac, _ci
            });

            if (cursor != null && cursor.getCount() > 0) {
                return true;
            } else
                return false;
        } catch (Exception e) {
            Log.d(TAG, "hasCellInfoBeanByTimesID Exception=>" + e.getMessage());
            return false;
        } finally {
            try {
                if (cursor != null) {
                    cursor.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                if (db != null) {
                    db.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 根据一次cursor返回cellInfoBean对象
     * 
     * @param cursor
     * @param roundInfoBean
     */
    private NetTestCellInfoBean dbSetCellInfoBean(Cursor cursor) {
        NetTestCellInfoBean cellInfoBean = new NetTestCellInfoBean();
        cellInfoBean.setPID(cursor.getInt(cursor
                .getColumnIndex(NetTestDBConstant.CellInfoFiledName.pid)));
        try {
            cellInfoBean.setAverageSpeed(Double.parseDouble(cursor.getString(cursor
                    .getColumnIndex(NetTestDBConstant.CellInfoFiledName.averageSpeed))));
        } catch (Exception e) {
            cellInfoBean.setAverageSpeed(0.0);
        }
        try {
            cellInfoBean.setMaxSpeed(Double.parseDouble(cursor.getString(cursor
                    .getColumnIndex(NetTestDBConstant.CellInfoFiledName.maxSpeed))));
        } catch (Exception e) {
            cellInfoBean.setMaxSpeed(0.0);
        }
        try {
            cellInfoBean.setMinSpeed(Double.parseDouble(cursor.getString(cursor
                    .getColumnIndex(NetTestDBConstant.CellInfoFiledName.minSpeed))));
        } catch (Exception e) {
            cellInfoBean.setMinSpeed(0.0);
        }
        try {
            cellInfoBean.setLac(Integer.parseInt(cursor.getString(cursor
                    .getColumnIndex(NetTestDBConstant.CellInfoFiledName.lac))));
        } catch (Exception e) {
            cellInfoBean.setLac(0);
        }
        try {
            cellInfoBean.setCi(Integer.parseInt(cursor.getString(cursor
                    .getColumnIndex(NetTestDBConstant.CellInfoFiledName.ci))));
        } catch (Exception e) {
            cellInfoBean.setCi(0);
        }
        cellInfoBean.setCellName(cursor.getString(cursor
                .getColumnIndex(NetTestDBConstant.CellInfoFiledName.cellName)));
        try {
            cellInfoBean.setMaxNetStrong(Integer.parseInt(cursor.getString(cursor
                    .getColumnIndex(NetTestDBConstant.CellInfoFiledName.maxNetStrong))));
        } catch (Exception e) {
            cellInfoBean.setMaxNetStrong(0);
        }
        try {
            cellInfoBean.setMinNetStrong(Integer.parseInt(cursor.getString(cursor
                    .getColumnIndex(NetTestDBConstant.CellInfoFiledName.minNetStrong))));
        } catch (Exception e) {
            cellInfoBean.setMinNetStrong(0);
        }
        try {
            cellInfoBean.setTimesID(Integer.parseInt(cursor.getString(cursor
                    .getColumnIndex(NetTestDBConstant.CellInfoFiledName.timesID))));
        } catch (Exception e) {
            cellInfoBean.setTimesID(0);
        }
        cellInfoBean.setIsDelete(cursor.getString(cursor
                .getColumnIndex(NetTestDBConstant.CellInfoFiledName.isDelete)));
        return cellInfoBean;
    }

    /**
     * 查询connect表所有数据
     * 
     * @return
     */
    public ArrayList<NetTestConnecInfoBean> queryAllConnectInfos() {
        SQLiteDatabase db = null;
        Cursor cursor = null;
        ArrayList<NetTestConnecInfoBean> netTestConnectInfoBeans = new ArrayList<NetTestConnecInfoBean>();
        try {
            db = getReadableDatabase();
            String sql = "select * from " + NetTestDBConstant.TABLES_CONNECTINFO_NAME;
            cursor = db.rawQuery(sql, null);

            if (cursor != null && cursor.getCount() > 0) {
                while (cursor.moveToNext()) {
                    NetTestConnecInfoBean connectInfoBean = new NetTestConnecInfoBean();
                    connectInfoBean.setPID(cursor.getInt(cursor
                            .getColumnIndex(NetTestDBConstant.ConnectFiledName.pid)));
                    try {
                        connectInfoBean.setLac(cursor.getInt(cursor
                                .getColumnIndex(NetTestDBConstant.ConnectFiledName.lac)));
                    } catch (Exception e) {
                        connectInfoBean.setLac(0);
                    }
                    try {
                        connectInfoBean.setCi(cursor.getInt(cursor
                                .getColumnIndex(NetTestDBConstant.ConnectFiledName.ci)));
                    } catch (Exception e) {
                        connectInfoBean.setCi(0);
                    }
                    try {
                        connectInfoBean
                                .setConnectPercent(cursor.getInt(cursor
                                        .getColumnIndex(NetTestDBConstant.ConnectFiledName.connectPercent)));
                    } catch (Exception e) {
                        connectInfoBean.setConnectPercent(0);
                    }
                    try {
                        connectInfoBean.setDropPercent(cursor.getInt(cursor
                                .getColumnIndex(NetTestDBConstant.ConnectFiledName.dropPercent)));
                    } catch (Exception e) {
                        connectInfoBean.setDropPercent(0);
                    }
                    netTestConnectInfoBeans.add(connectInfoBean);
                }
                if (netTestConnectInfoBeans.size() > 0)
                    return netTestConnectInfoBeans;
                else
                    return null;
            } else
                return null;
        } catch (Exception e) {
            Log.d(TAG, "queryAllConnectInfos Exception=>" + e.getMessage());
            return null;
        } finally {
            try {
                if (cursor != null) {
                    cursor.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                if (db != null) {
                    db.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 根据小区lac和ci计算小区的接通率
     * 
     * @param _lac
     * @param _ci
     * @return
     */
    public double queryCellConnectPercent(int _lac, int _ci) {
        int allConnect = 0, successConnect = 0;
        SQLiteDatabase db = null;
        Cursor cursor = null;
        try {
            db = getReadableDatabase();
            String sql = "select * from " + NetTestDBConstant.TABLES_CONNECTINFO_NAME + " where "
                    + NetTestDBConstant.ConnectFiledName.lac + " =? and "
                    + NetTestDBConstant.ConnectFiledName.ci + " =? order by pid";
            cursor = db.rawQuery(sql, new String[] {
                    String.valueOf(_lac), String.valueOf(_ci)
            });

            if (cursor != null && cursor.getCount() > 0) {
                while (cursor.moveToNext()) {
                    allConnect++;
                    if (cursor
                            .getString(
                                    cursor.getColumnIndex(NetTestDBConstant.ConnectFiledName.connectPercent))
                            .equals("0")) {
                        successConnect++;
                    }
                }
                return (double) (successConnect / allConnect);
            } else
                return 0;
        } catch (Exception e) {
            Log.d(TAG, "queryCellConnectPercent Exception=>" + e.getMessage());
            return 0;
        } finally {
            try {
                if (cursor != null) {
                    cursor.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                if (db != null) {
                    db.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 根据小区lac和ci计算小区的掉线率
     * 
     * @param _lac
     * @param _ci
     * @return
     */
    public double queryCellDropPercent(int _lac, int _ci) {
        int allConnect = 0, dropConnect = 0;
        SQLiteDatabase db = null;
        Cursor cursor = null;
        try {
            db = getReadableDatabase();
            String sql = "select * from " + NetTestDBConstant.TABLES_CONNECTINFO_NAME + " where "
                    + NetTestDBConstant.ConnectFiledName.lac + " =? and "
                    + NetTestDBConstant.ConnectFiledName.ci + " =? order by pid";
            cursor = db.rawQuery(sql, new String[] {
                    String.valueOf(_lac), String.valueOf(_ci)
            });

            if (cursor != null && cursor.getCount() > 0) {
                while (cursor.moveToNext()) {
                    allConnect++;
                    if (cursor.getString(
                            cursor.getColumnIndex(NetTestDBConstant.ConnectFiledName.dropPercent))
                            .equals("0")) {
                        dropConnect++;
                    }
                }
                return (double) (dropConnect / allConnect);
            } else
                return 0;
        } catch (Exception e) {
            Log.d(TAG, "queryCellDropPercent Exception=>" + e.getMessage());
            return 0;
        } finally {
            try {
                if (cursor != null) {
                    cursor.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                if (db != null) {
                    db.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 传入_roundID,修改该round的信息isdelete为1，表示已删除
     * 
     * @param _roundID
     * @return
     */
    public boolean deleteRoundInfo(int _roundID) {
        SQLiteDatabase db = null;
        try {
            db = getWritableDatabase();
            ContentValues values = new ContentValues();
            values.put(NetTestDBConstant.TestRoundFiledName.isDelete, "1");
            db.update(NetTestDBConstant.TABLES_TESTROUNDINFO_NAME, values,
                    NetTestDBConstant.TestRoundFiledName.pid + "=?", new String[] {
                        String.valueOf(_roundID)
                    });
            return true;
        } catch (Exception e) {
            Log.d(TAG, "deleteRoundInfo Exception=>" + e.getMessage());
            return false;
        } finally {
            try {
                if (db != null) {
                    db.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        Log.i(TAG, "nettestdb onCreate");
        createTestInfoTables(db);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.i(TAG, "nettestdb onUpgrade");
        createTestInfoTables(db);
        // createTestInfoTables(db);
        // db.execSQL("DROP TABLE IF EXISTS " +
        // NetTestDBConstant.TABLES_TESTROUNDINFO_NAME);
        // db.execSQL("DROP TABLE IF EXISTS " +
        // NetTestDBConstant.TABLES_TESTTIMEINFO_NAME);
        // db.execSQL("DROP TABLE IF EXISTS " +
        // NetTestDBConstant.TABLES_TESTCELLINFO_NAME);
        // db.execSQL("DROP TABLE IF EXISTS " +
        // NetTestDBConstant.TABLES_CONNECTINFO_NAME);
        // createTestRoundInfoTable(db);
        // createTestTimesInfoTable(db);
        // createTestCellInfoTable(db);
        // createTestConnectInfoTable(db);
    }

    /**
     * 查询表最后一条记录的pid值
     * 
     * @param tablename
     * @return 如果有pid值直接返回，没有返回0，表示表内目前没有记录
     */
    public int selectLastRowPid(String tablename) {
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = null;
        try {
            int pid;
            String sql = "select * from " + tablename + " order by pid desc LIMIT 1";
            cursor = db.rawQuery(sql, null);

            if (cursor != null && cursor.getCount() > 0) {
                cursor.moveToFirst();
                int pidindex = cursor.getColumnIndex("pid");
                // pid = cursor.getString(pidindex);
                pid = cursor.getInt(pidindex);
                return pid;
            } else {
                return 0;
            }
        } catch (Exception ex) {
            Log.d(TAG, "selectLastRowPid Exception=>" + ex.getMessage());
            return 0;
        } finally {
            try {
                if (cursor != null) {
                    cursor.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                if (db != null) {
                    db.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 判断某张表是否存在
     * 
     * @param tabName 表名
     * @return boolean true存在，false不存在
     */
    public boolean tabbleIsExist(String tableName) {
        boolean result = false;
        if (tableName == null) {
            return false;
        }
        SQLiteDatabase db = null;
        Cursor cursor = null;
        try {
            if (db == null)
                db = getReadableDatabase();
            String sql = "select count(*) as c from Sqlite_master  where type ='table' and name ='"
                    + tableName.trim() + "' ";
            cursor = db.rawQuery(sql, null);
            if (cursor.moveToNext()) {

                if (cursor != null && cursor.getCount() > 0) {
                    result = true;
                }
            }
        } catch (Exception e) {
            return false;
        } finally {
            try {
                if (cursor != null) {
                    cursor.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    /**
     * 查询表是否包含某个字段
     * 
     * @param tablename 表名，filedName 查询的字段
     * @return boolean true有这个字段，false没有这个字段
     */
    public boolean queryField(String tablename, String filedName) {
        SQLiteDatabase db = null;
        Cursor cursor = null;
        try {
            db = this.getReadableDatabase();
            String sql = "select * from sqlite_master where tbl_name ='" + tablename
                    + "' and sql like '%" + filedName + "%'";
            cursor = db.rawQuery(sql, new String[] {});

            if (cursor != null && cursor.getCount() > 0) {
                return true;
            } else {
                return false;
            }
        } catch (Exception ex) {
            Log.i("GYH", ex.getMessage());
            return false;
        } finally {
            try {
                if (cursor != null) {
                    cursor.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                if (db != null) {
                    db.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /*
     * 获取小区中文名
     */
    public String getCellName(Context context, int lac, int ci, String netType) {
        try {
            DBCellsHelper dbcellHelper = new DBCellsHelper(context);
            return dbcellHelper.queryCellName(lac, ci, netType);
        } catch (Exception ex) {
            return "";
        }
    }

}
